<?php
namespace app\admin\controller;
use Qiniu\Auth as Auth;
use Qiniu\Storage\UploadManager;
//规格管理
class Img extends Common
{
	//标签列表
    public function img_list()
    {
		$where=array('a.gi_id'=>array('gt',0));
        $name=Request()->param('name');
		//名称
		$status=Request()->param('status');
		//状态
        $start_time=strtotime(Request()->param('start_time'));
        $end_time=strtotime(Request()->param('end_time'));
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
		if($name!=''){
			$where['b.g_name']=array('like','%'.$name.'%');
		}
        $where['a.gi_status'][]=array('neq',3);
		if($status>0){
			if($status==1){
				$where['a.gi_status'][]=1;
			}else{
				$where['a.gi_status'][]=2;
			}
		}else{
            $where['a.gi_status'][]=array('egt',0);
        }
        $where_time=array(array('gt',0),array('gt',0));
        if($start_time>0){
            $where_time[]=array('egt',$start_time);
        }else{
            $start_time='';
        }
        if($end_time>0){
            $where_time[]=array('elt',$end_time);
        }else{
            $end_time='';
        }
        $where['a.gi_create_time']=$where_time;
        $img=db('goods_img a')
            ->field("a.*,b.g_name")
            ->join('fr_goods b','a.g_id=b.g_id','left')
			->order('a.gi_sort desc,a.gi_status')
			->where($where)
            ->limit("$now,$size")
			->select();
		//标签列表
        $count=db('goods_img a')
            ->join('fr_goods b','a.g_id=b.g_id','left')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($img));//分页
        $this->view->assign('page_str',$page_str);
		$this->view->assign('data',$img);
		$this->assign('status',$status);
		$this->assign('name',$name);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();

    }
	//添加图片
	public function add_img(){
		if(request()->isGet()){
			//get，加载页面
            $id=request()->get('id');
            $goods=db('goods')
                ->where(array('g_id'=>$id))
                ->find();
            $this->assign('goods',$goods);
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();
			$id=$data['id'];
            unset($data['id']);
            $data1=array();
            for($i=1;$i<=5;$i++){
                $file =Request()->file("gi_img_".$i);
                if($file){
//                    $info = $file->move(ROOT_PATH.'public/upload/goods_img'.DS);
//                    $img="/upload/goods_img".DS.$info->getSaveName();
                    //以下为图片上传到其他地址(阿里云)
                    $upload = new Upload();
                    $return=$upload->upload($file);
                    if($return['code']!=1){
                        return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传图片');
                    }else{
                        $img=$return['url'];
                    }
                    //以上为图片上传到其他地址(阿里云)
                    if($data['gi_sort_'.$i]<=0){
                        $sort=0;
                    }else{
                        $sort=$data['gi_sort_'.$i];
                    }
                    $status=$data['gi_status_'.$i];
                    $data1[]=array('g_id'=>$id,'gi_img'=>$img,'gi_sort'=>$sort,'gi_status'=>$status,'gi_add_user_id'=>session('uid'),'gi_create_time'=>time(),'gi_add_user_name'=>session('user.real_name'));
                }
            }
            if(count($data1)<1){
                return return_json(array('code'=>0,'msg'=>'请至少上传一张图片'),'添加图片');
            }
            $res=db('goods_img')->insertAll($data1);
            //添加图片
            if($res!=false){
                return return_json(array('code'=>1,'msg'=>'图片添加成功'),'添加图片');
            }else{
                return return_json(array('code'=>0,'msg'=>'图片添加失败'),'添加图片');
            }
		}
    }
    //修改图片
    function reimg(){
        if(request()->isGet()){
            //get，加载页面
            $id=request()->get('id');
            $goods_img=db('goods_img a')
                ->field("a.*,b.g_name")
                ->join("fr_goods b","a.g_id = b.g_id","left")
                ->where(array('a.gi_id'=>$id))
                ->find();
            $this->assign('goods_img',$goods_img);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $id=$data['id'];
            unset($data['id']);
            $goods_img=db('goods_img a')
                ->where(array('a.gi_id'=>$id))
                ->find();
            if(empty($goods_img)){
                return return_json(array('code'=>0,'msg'=>'没有该图片'),'修改展示图片');
            }
            $file =Request()->file("gi_img");
            if($file){
//                $info = $file->move(ROOT_PATH.'public/upload/goods_img'.DS);
//                $img="/upload/goods_img".DS.$info->getSaveName();
                //以下为图片上传到其他地址(阿里云)
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    return return_json(array('code'=>0,'msg'=>'图片上传失败:'.$return['msg']),'上传图片');
                }else{
                    $img=$return['url'];
                }
                //以上为图片上传到其他地址(阿里云)
                $data['gi_img']=$img;
            }
            if($data['gi_sort']<=0){
                $data['gi_sort']=0;
            }
            $data['gi_update_time']=time();
            unset($data['uploadfile']);
            $res=db('goods_img')->where(array('gi_id'=>$id))->update($data);
            //添加权限
            if($res!==false){
                //if($file){
                //    unlink(ROOT_PATH.'public/'.$goods_img['gi_img']);
                //}
                return return_json(array('code'=>1,'msg'=>'展示图片修改成功'),'修改展示图片');
            }else{
                return return_json(array('code'=>0,'msg'=>'展示图片修改失败'),'修改展示图片');
            }
        }
    }
    //图片禁用
    function img_del(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('goods_img')
                ->where(array('gi_id'=>$id,'gi_status'=>array('neq',3)))
                ->update(array('gi_status'=>2,'gi_update_time'=>time()));
            if($res){
                return return_json(array('code'=>1,'msg'=>'禁用成功！'),'禁用展示图片');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'禁用失败！'),'禁用展示图片');die;
            }
        }
    }
    //图片启用
    function img_use(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $res=db('goods_img')
                ->where(array('gi_id'=>$id,'gi_status'=>array('neq',3)))
                ->update(array('gi_status'=>1,'gi_update_time'=>time()));
            if($res){
                return return_json(array('code'=>1,'msg'=>'启用成功！'),'启用展示图片');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'启用失败！'),'启用展示图片');die;
            }
        }
    }
    //图片删除
    function img_real_del(){
        if(Request()->isAjax()){
            $id=Request()->post('id');
            $img=db('goods_img')
                ->where(array('gi_id'=>$id))
                ->find();
            if($img['gi_status']==3){
                return return_json(array('code'=>1,'msg'=>'删除成功！'),'删除展示图片');die;
            }
            $res=db('goods_img')
                ->where(array('gi_id'=>$id))
                ->update(array('gi_status'=>3,'gi_update_time'=>time()));
            if($res){
                //unlink(ROOT_PATH.'public/'.$img['gi_img']);
                //删除图片
                return return_json(array('code'=>1,'msg'=>'删除成功！'),'删除展示图片');die;
            }else{
                return return_json(array('code'=>0,'msg'=>'删除失败！'),'删除展示图片');die;
            }
        }
    }
}
